﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IReservoirServices;
using MaiPlanServer.IServices.IWarehouseServives;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.ReservoirServices
{
    public class ReservoirManageServices: IReservoirManageServices
    {
        public ReservoirManageServices(IBaseRepository BaseConn) {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }

        public async Task<bool> AddAsync(WM_STORAGE_LOCATION model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }

        public async Task<bool> DellAllAsync(string[] ruleId)
        {
            string sql = "delete WM_STORAGE_LOCATION where LOCATION_ID in @ruleId";
            var result = await BaseConn.DeleteAsync(sql, new { ruleId });
            return result;
        }

        public async Task<bool> DellAsync(string ruleId)
        {
            string sql = "delete WM_STORAGE_LOCATION where LOCATION_ID =@ruleId";
            var result = await BaseConn.DeleteAsync(sql, new { ruleId });
            return result;
        }

        public async Task<IEnumerable<WM_STORAGE_LOCATION>> GetArmazemAsync(int PageIndex, int PageSize, string LOCATIONName, string LOCATIONcode,string warehouse_id) 
        {
            string sql = "select * from WM_STORAGE_LOCATION where 1=1 and LOCATION_name like @LOCATIONName and   LOCATION_code like @LOCATIONcode";


            if (!string.IsNullOrEmpty(warehouse_id))
            {
                sql += " and warehouse_id=@warehouse_id ";
            }

            sql += " order by Create_Time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";

            var list = await BaseConn.QueryAsync<WM_STORAGE_LOCATION>(sql, new { PageIndex, PageSize, LOCATIONName = "%" + LOCATIONName + "%", LOCATIONcode = "%" + LOCATIONcode + "%" , warehouse_id = warehouse_id });
            return list;
        }

        public async Task<int> GetCountAsync(string LOCATIONName,string warehouse_id)
        {
            string sql = @" select count(1) from WM_STORAGE_LOCATION where  LOCATION_name like  @LOCATIONName  ";
            if (!string.IsNullOrEmpty(warehouse_id))
            {
                sql += " and warehouse_id=@warehouse_id ";
            }
            var conut = await BaseConn.GetRecoreCountAsync(sql, new { LOCATIONName = "%" + LOCATIONName + "%" , warehouse_id = warehouse_id });
            return conut;
        }

        public async Task<IEnumerable<WM_STORAGE_LOCATION>> GetLocation(int PageIndex, int PageSize, string id)
        {
            
            var sql = "select * from WM_STORAGE_LOCATION where 1=1 ";
            if (!string.IsNullOrEmpty(id))
            {
                sql += " and WAREHOUSE_ID=@id";
            }
            sql += " order by Create_Time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";
            var resilt = await BaseConn.QueryAsync<WM_STORAGE_LOCATION>(sql, new { PageIndex, PageSize, id});
            return resilt;
        }

        public async Task<int> GetLocationcount(string id)
        {
            var sql = "select count(1) from WM_STORAGE_LOCATION where WAREHOUSE_ID=@id";
            var count = await BaseConn.GetRecoreCountAsync(sql, new {id });
            return count;
        }


        public async Task<IEnumerable<wm_warehouse>> Getwarehouse()
        {
            var sql = "select Warehouse_id,Warehouse_name from wm_warehouse";
            var resilt = await BaseConn.QueryAllAsync<wm_warehouse>(sql);
            return resilt;
        }

        public async Task<bool> UpdateAsync(WM_STORAGE_LOCATION model)
        {
            var result = await BaseConn.UpdateAsync(model, "LOCATION_ID");

            return result;
        }
    }
}
